package com.light;

import atlantafx.base.controls.ModalPane;
import atlantafx.base.theme.*;
import com.light.layout.ContentPane;
import com.light.layout.MenuPane;
import com.light.thread.FxAsyncTask;
import com.light.thread.FxThreadPoolTask;
import com.light.util.FxApplicationContextUtils;
import com.light.util.FxUtil;
import com.light.util.H2PoolUtils;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GitManagerApp extends Application {

    public static final Logger LOGGER = LoggerFactory.getLogger(GitManagerApp.class);

    private static final String STYLE_SHEET = FxUtil.getResource("/css/root.css");

    private long startTime;

    @Override
    public void init() throws Exception {
        startTime = System.currentTimeMillis();
        super.init();

        FxAsyncTask.runOnceBack(startTime, "初始化数据库和主题数据",
                () -> {
                    String gitDbInit = H2PoolUtils.queryDictByLabel("GIT_DB_INIT", "0");
                    if ("0".equals(gitDbInit)) {
                        H2PoolUtils.createTable();
                        H2PoolUtils.initGitProjectDictData();
                    }

                    FxApplicationContextUtils.THEME_LIST.add(new PrimerLight());
                    FxApplicationContextUtils.THEME_LIST.add(new PrimerDark());
                    FxApplicationContextUtils.THEME_LIST.add(new NordLight());
                    FxApplicationContextUtils.THEME_LIST.add(new NordDark());
                    FxApplicationContextUtils.THEME_LIST.add(new CupertinoLight());
                    FxApplicationContextUtils.THEME_LIST.add(new CupertinoDark());
                    FxApplicationContextUtils.THEME_LIST.add(new Dracula());
                    // 设置主题
                    return H2PoolUtils.queryDictByLabel("GIT_CURRENT_THEME", "Primer Light");
                },
                success -> {
                    FxApplicationContextUtils.CURRENT_THEME_NAME.set(success);
                    FxApplicationContextUtils.THEME_LIST.stream()
                            .filter(theme -> theme.getName().equals(success))
                            .findFirst()
                            .ifPresentOrElse(theme -> Application.setUserAgentStylesheet(theme.getUserAgentStylesheet()),
                                    () -> Application.setUserAgentStylesheet(FxApplicationContextUtils.THEME_LIST.getFirst().getUserAgentStylesheet())
                            );

                    // 查询所有项目数据
                    H2PoolUtils.queryGitProjects();
                });
    }

    @Override
    public void start(Stage stage) throws Exception {
        Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
        // 根节点
        StackPane root = new StackPane();

        // 遮罩层
        var modalPane = new ModalPane();
        modalPane.setId(FxUtil.MAIN_MODAL_ID);

        // 左右布局
        HBox container = new HBox();
        // 内容
        ContentPane contentPane = new ContentPane();
        // 菜单
        MenuPane menuPane = new MenuPane(contentPane);
        container.getChildren().addAll(menuPane, contentPane);
        HBox.setHgrow(contentPane, Priority.ALWAYS);

        root.getChildren().addAll(modalPane, container);

        // 场景
        Scene scene = new Scene(root);
        stage.setTitle("Git批量管理工具");
        stage.setMinWidth(1100);
        stage.setMinHeight(650);
        stage.getIcons().add(FxUtil.getImage("/icons/git.png"));
        stage.setScene(scene);
        stage.show();
        // 加载样式，并将scene保存到全局容器变量中
        scene.getStylesheets().add(STYLE_SHEET);
        FxApplicationContextUtils.GLOBAL_CONTEXT_MAP.put("scene", scene);
        LOGGER.info("项目启动完成，耗时 {} ms", System.currentTimeMillis() - startTime);
    }

    @Override
    public void stop() throws Exception {
        super.stop();
        H2PoolUtils.closeConnectionPool();
        FxThreadPoolTask.close();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
